package com.fromdev.automation;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestCase;

public class StumbleHopper extends SeleneseTestCase {
	private String PASSWORD = "pass";
	private String UUSER_EMAIL = "a@b.com";

	@Before
	public void setUp() throws Exception {
		selenium = new DefaultSelenium("localhost", 4444, "*googlechrome",
				"https://www.stumbleupon.com");
		initConfig();
		selenium.start();
	}

	private void initConfig() throws IOException {
		Properties prop = new Properties();		  
		prop.load(this.getClass().getClassLoader().getResourceAsStream("config.properties"));
		PASSWORD = prop.getProperty("user.password");
		UUSER_EMAIL = prop.getProperty("user.email");
	}

	@Test
	public void testStumbleHopper() throws Exception {
		int sleepTime = 0;
		int exploreCategory = 0;
		int freshLenseId = 0;
		
		selenium.open("/");
		selenium.waitForPageToLoad("30000");
		try {
			login();

			Thread.sleep(TimeUtil.getTypingTimeSeconds());
			
			//repeat other steps until favorable
			while (true) {
				try {
					if (TimeUtil.isWeekDay() && !TimeUtil.isWorkingHour()) {
						// pick a random category to explore
						//exploreCategory = TimeUtil.getNumberBetween(1, 25);
						//exploreCategories(exploreCategory);

						freshLenseId = TimeUtil.getNumberBetween(1, 20);
						openFreshLense(freshLenseId);
						readFullPage();
						// Randomly like a lense
						if (TimeUtil.getNumberBetween(1, 25) % 2 == 0) {
							likeLense();
						} else {
							System.out.println("I dont like this lense");
						}
					} else {
						System.out.println("Nothing to do");
					}
					sleepTime = TimeUtil.getTypingTimeSeconds()* 15;
					System.out.println("Done: Going to sleep for " + sleepTime);
					Thread.sleep(sleepTime);
				} catch (Exception e) {
					e.printStackTrace();
					System.out.println("Failed for: exploreCategory:" + exploreCategory + " FreshLenseId:" + freshLenseId );
					if (!goToHome()) {
						System.out
								.println("No Home Page link available. Finishing job.");
						break;
					}
				} finally {
					System.out.println("Finally Done one round");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Failed for: exploreCategory:" + exploreCategory + " FreshLenseId:" + freshLenseId );
		}

	}

	private void login() throws InterruptedException {
		List singInXPathList = new ArrayList();
		singInXPathList.add("//*[@id='header-top']/div/a[2]/strong");		
		singInXPathList.add("link=Sign in");
		
		if (SeleniumUtil.doClick(selenium, singInXPathList)) {
			Thread.sleep(TimeUtil.getTypingTimeSeconds());
			selenium.click("id=user");
			selenium.type("id=user", UUSER_EMAIL);
			Thread.sleep(TimeUtil.getTypingTimeSeconds());
			selenium.type("id=pass", PASSWORD);
			Thread.sleep(TimeUtil.getTypingTimeSeconds());
			selenium.click("id=login");
			selenium.waitForPageToLoad("30000");
			System.out.println("Just Logged in");
			if (selenium.isElementPresent("//*[@id='dash_logo']/a")) {
				selenium.click("//*[@id='dash_logo']/a");
				selenium.waitForPageToLoad("30000");
			}
		}
	}


	private void readFullPage() throws InterruptedException {
		int sleepTime;
		//if (selenium.isElementPresent("//*[@id='footer']/div[1]/div[1]")) {
		//	selenium.click("//*[@id='footer']/div[1]/div[1]");
			// selenium.waitForPageToLoad("30000");
			sleepTime = TimeUtil.getTypingTimeSeconds() + 15000;
			System.out.println("readFullPage: Reading the Lense till footer " + sleepTime);
			Thread.sleep(sleepTime);
	//		System.out.println("Read the Lense till footer ");
	//	}
	}

	private void openFreshLense(int freshLenseId) throws InterruptedException {
		int sleepTime;
		if (selenium.isElementPresent("//*[@id='tb-stumble']/a")) {
			selenium
					.click("//*//*[@id='tb-stumble']/a");
			selenium.waitForPageToLoad("30000");
			sleepTime = TimeUtil.getTypingTimeSeconds();
			System.out.println("openFreshLense: Going to sleep for " + sleepTime);
			Thread.sleep(sleepTime);
			System.out.println("Clicked on Fresh lense");
		}
	}

	private void likeLense() throws InterruptedException {
		int sleepTime;
		String likeButtonXPath = "css=#tb-like > a > span.tb-btn-ico";
		if (selenium.isElementPresent(likeButtonXPath)) {
			selenium.click(likeButtonXPath);
			// selenium.waitForPageToLoad("30000");
			sleepTime = TimeUtil.getTypingTimeSeconds();
			System.out.println("likeLense: Going to sleep for " + sleepTime);
			Thread.sleep(sleepTime);
			System.out.println("I like this lense");
		}
	}

	private boolean goToHome() {
		try {
		if (selenium.isElementPresent("//*[@id='header']/h4/a")) {
			selenium.click("//*[@id='header']/h4/a");
			selenium.waitForPageToLoad("30000");
			System.out.println("Click Back to Home Page");
			return true;
		} else {
			selenium.open("/");
			selenium.waitForPageToLoad("30000");
			System.out.println("/ to Home Page");
			return true;
		}
		}catch(Exception e) {
			System.out.println("Cant find home page");
		}
		return false;
	}

	@After
	public void tearDown() throws Exception {
		selenium.stop();
	}
}
